import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.List;

public class MainLib {
    private MysqlTushu mysqlTushu;
    private Set<Integer> idSet = new HashSet<>();
    private Set<Massage> massages = new HashSet<>();
    private List<Massage> Sreach = new ArrayList<>();
    private List<Massage> list = new ArrayList<>();
    private JList<Massage> tmp;
    public JPanel panel = new JPanel();

    public int size;
    public int index = 0;
    public int sortIndex = 0;
    public int showIndex = 0;
    public int chooiceIndex = -1;
    public String sreachString = new String();

    public static int autoId = 0;
    public static final int N = 15;

    MainLib(String user,String passwd) throws Exception {
        mysqlTushu = new MysqlTushu(user,passwd);
        initalize();
    }

    private void tmpInitalize() {
        for (Massage m : TestMassagesDrive.set) {
            addMassage(m);
        }
    }

    private void initalize() {
        //tmpInitalize();
        mysqlTushu.sqlInit(massages,idSet);
    }

    public boolean judge(int id) {
        return !idSet.contains(id);
    }

    private int getAutoId() {
        while (judge(autoId) == false) {
            if(autoId == Integer.MAX_VALUE){
                autoId = 0;
            }
            autoId++;
        }
        return autoId;
    }

    public void addMassage(Massage massage) {
        if (judge(massage.getId()) == false) {
            massage.changId(getAutoId());
        }
        idSet.add(massage.getId());
        massages.add(massage);
        if(KMPMatcher.search(massage.toString(),sreachString) != -1){
            Sreach.add(massage);
        }
        mysqlTushu.insert(massage);
    }

    public void delMassage(int id) {
        if (id == -1) {
            return;
        }
        idSet.remove(list.get(id).getId());
        massages.remove(list.get(id));
        mysqlTushu.erase(list.get(id).getId());

        Sreach.clear();
        for (Massage m : massages) {
            if (KMPMatcher.search(m.toString(), sreachString) != -1) {
                Sreach.add(m);
            }
        }
        index = 0;
    }

    public void lendMassage(int id) {
        if (id == -1) {
            return;
        }
        list.get(id).setLendStatu(true);
    }

    public void retMassage(int id) {
        if (id == -1) {
            return;
        }
        list.get(id).setLendStatu(false);
    }

    public void upSreach(String tag, LibManage.EventPage v) {
        Sreach.clear();
        for (Massage m : massages) {
            if (KMPMatcher.search(m.toString(), tag) != -1) {
                Sreach.add(m);
            }
        }
        index = 0;
        switchShow(showIndex);
        switchSort(sortIndex, v);
    }

    public JList<Massage> retJList() {
        return new JList(massages.toArray());
    }

    public void switchShow(int show) {
        switch (show) {
            case 0:
                defaultShowData();
                break;
            case 1:
                showLend();
                break;
            case 2:
                showLib();
                break;
        }
    }

    public void showLend() {
        list = new ArrayList<>();
        for (Massage m : Sreach) {
            if (m.getLendStatu() == true) {
                list.add(m);
            }
        }
    }

    public void showLib() {
        list = new ArrayList<>();
        for (Massage m : Sreach) {
            if (m.getLendStatu() == false) {
                list.add(m);
            }
        }
    }

    public void defaultShowData() {
        list = new ArrayList<>(Sreach);
    }

    public void switchSort(int i, LibManage.EventPage v) {
        switch (i) {
            case 0:
                idSort(v);
                break;
            case 1:
                idReSort(v);
                break;
            case 2:
                nameSort(v);
                break;
            case 3:
                nameReSort(v);
                break;
            case 4:
                authorSort(v);
                break;
            case 5:
                authorReSort(v);
                break;
            case 6:
                priceSort(v);
                break;
            case 7:
                priceReSort(v);
                break;
        }
    }

    public void switchSort(int i) {
        switch (i) {
            case 0:
                idSort();
                break;
            case 1:
                idReSort();
                break;
            case 2:
                nameSort();
                break;
            case 3:
                nameReSort();
                break;
            case 4:
                authorSort();
                break;
            case 5:
                authorReSort();
                break;
            case 6:
                priceSort();
                break;
            case 7:
                priceReSort();
                break;
        }
    }

    private void nameSort(LibManage.EventPage v) {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                int j = SortCompare.name.compare(o1, o2);
                if (j == 0) {
                    return SortCompare.author.compare(o1, o2);
                } else {
                    return j;
                }
            }
        });
        initRetList(v);
    }

    private void nameReSort(LibManage.EventPage v) {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                int j = SortCompare.name.compare(o2, o1);
                if (j == 0) {
                    return SortCompare.author.compare(o1, o2);
                } else {
                    return j;
                }
            }
        });
        initRetList(v);
    }

    private void authorSort(LibManage.EventPage v) {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                int j = SortCompare.author.compare(o1, o2);
                if (j == 0) {
                    return SortCompare.name.compare(o1, o2);
                } else {
                    return j;
                }
            }
        });
        initRetList(v);
    }

    private void authorReSort(LibManage.EventPage v) {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                int j = SortCompare.author.compare(o2, o1);
                if (j == 0) {
                    return SortCompare.name.compare(o1, o2);
                } else {
                    return j;
                }
            }
        });
        initRetList(v);
    }

    private void priceSort(LibManage.EventPage v) {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                int j = SortCompare.price.compare(o1, o2);
                if (j == 0) {
                    return SortCompare.name.compare(o1, o2);
                } else {
                    return j;
                }
            }
        });
        initRetList(v);
    }

    private void priceReSort(LibManage.EventPage v) {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                int j = SortCompare.price.compare(o2, o1);
                if (j == 0) {
                    return SortCompare.name.compare(o1, o2);
                } else {
                    return j;
                }
            }
        });
        initRetList(v);
    }

    private void idSort(LibManage.EventPage v) {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                return SortCompare.id.compare(o1, o2);
            }
        });
        initRetList(v);
    }

    private void idReSort(LibManage.EventPage v) {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                return SortCompare.id.compare(o2, o1);
            }
        });
        initRetList();
    }

    private void nameSort() {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                int j = SortCompare.name.compare(o1, o2);
                if (j == 0) {
                    return SortCompare.author.compare(o1, o2);
                } else {
                    return j;
                }
            }
        });
        initRetList();
    }

    private void nameReSort() {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                int j = SortCompare.name.compare(o2, o1);
                if (j == 0) {
                    return SortCompare.author.compare(o1, o2);
                } else {
                    return j;
                }
            }
        });
        initRetList();
    }

    private void authorSort() {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                int j = SortCompare.author.compare(o1, o2);
                if (j == 0) {
                    return SortCompare.name.compare(o1, o2);
                } else {
                    return j;
                }
            }
        });
        initRetList();
    }

    private void authorReSort() {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                int j = SortCompare.author.compare(o2, o1);
                if (j == 0) {
                    return SortCompare.name.compare(o1, o2);
                } else {
                    return j;
                }
            }
        });
        initRetList();
    }

    private void priceSort() {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                int j = SortCompare.price.compare(o1, o2);
                if (j == 0) {
                    return SortCompare.name.compare(o1, o2);
                } else {
                    return j;
                }
            }
        });
        initRetList();
    }

    private void priceReSort() {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                int j = SortCompare.price.compare(o2, o1);
                if (j == 0) {
                    return SortCompare.name.compare(o1, o2);
                } else {
                    return j;
                }
            }
        });
        initRetList();
    }

    private void idSort() {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                return SortCompare.id.compare(o1, o2);
            }
        });
        initRetList();
    }

    private void idReSort() {
        switchShow(showIndex);
        list.sort(new Comparator<Massage>() {
            public int compare(Massage o1, Massage o2) {
                return SortCompare.id.compare(o2, o1);
            }
        });
        initRetList();
    }

    public void initRetList() {
        chooiceIndex = -1;
        if (tmp != null)
            panel.remove(tmp);
        size = list.size();
        Massage[] t = new Massage[N];

        int k = size / N;
        if (size % N != 0) {
            k++;
        }
        k = Math.max(1, k);
        if (index >= k) {
            index = k - 1;
        }
        for (int i = 0; i < N; i++) {
            int j = index * N + i;
            if (j >= list.size()) {
                break;
            }
            t[i] = list.get(j);
        }
        tmp = new JList<>(t);
        tmp.setFixedCellWidth(670);
        tmp.setFixedCellHeight(31);
        tmp.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    int i = tmp.locationToIndex(e.getPoint()) + index * N;
                    if (i < list.size()) {
                        chooiceIndex = i;
                    } else {
                        chooiceIndex = -1;
                    }
                }
            }
        });
        tmp.setFont(new Font("微软雅黑", Font.BOLD, 13));
        // tmp.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        panel.add(tmp);
    }

    public void initRetList(LibManage.EventPage v) {
        initRetList();
        v.chang();
    }

}